#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <net/if_arp.h>		/* For ARPHRD_ETHER */
#include <sys/socket.h>		/* For AF_INET & struct sockaddr */
#include <netinet/in.h>         /* For struct sockaddr_in */
#include <netinet/if_ether.h>

#include <linux/types.h>		/* for "caddr_t" et al		*/
#include <linux/socket.h>		/* for "struct sockaddr" et al	*/
#include <linux/if.h>			/* for IFNAMSIZ and co... */

#include <linux/wireless.h>

#include "sms.h"

#define WIFI_SETTING_LAYOUT_NAME		"layout-wifi-setting"

#define WIRELESS_LAST_CONF "/etc/wireless-last.conf"
#define NET_CONFIG_FOLDER       "/etc/network/"
#define WEP_CHECK



enum
{
    COL_ESSID = 0,
    COL_MODE,
    COL_KEY,
    COL_QUALITY,
    COL_STATE,
    n_cols
};

enum
{
    LINK_UP=1,
    LINK_DOWN,
    NO_LINK
};


typedef enum
{
    RESPONSE_DHCP = 1,
    RESPONSE_STATIC
}Wifi_Response;


static gboolean wifi_setting_layout_load_bg(xmlNode *a_node,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  wifilayout->bg_wifi= (BackGround_Wifi *)g_malloc0 (sizeof(BackGround_Wifi)+1);
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  if(wifilayout->bg_wifi->bg||!a_node)
    return FALSE;

  wifilayout->bg_wifi->bg_x1 = sms_get_xml_int(a_node,(xmlChar*)"x_1",251);
  wifilayout->bg_wifi->bg_y1 = sms_get_xml_int(a_node,(xmlChar*)"y_1",0);
  wifilayout->bg_wifi->bg_w1 = sms_get_xml_int(a_node,(xmlChar*)"w_1",550);
  wifilayout->bg_wifi->bg_h1 = sms_get_xml_int(a_node,(xmlChar*)"h_1",530);
  wifilayout->bg_wifi->bg_x2 = sms_get_xml_int(a_node,(xmlChar*)"x_2",0);
  wifilayout->bg_wifi->bg_y2 = sms_get_xml_int(a_node,(xmlChar*)"y_2",0);
  wifilayout->bg_wifi->bg_w2 = sms_get_xml_int(a_node,(xmlChar*)"w_2",1000);
  wifilayout->bg_wifi->bg_h2 = sms_get_xml_int(a_node,(xmlChar*)"h_2",530);	


  printf("bg_x1 = %d ,bg_x2 = %d\n" ,wifilayout->bg_wifi->bg_x1 ,wifilayout->bg_wifi->bg_x2);
  printf("bg_w2 = %d ,bg_h2 = %d\n" ,wifilayout->bg_wifi->bg_w2 ,wifilayout->bg_wifi->bg_h2);
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
    return FALSE;
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,800,480);
  if(!pixbuf)
    return FALSE;

  wifilayout->bg_wifi->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);	
  g_object_unref(pixbuf);

  val = xmlGetProp (a_node,(xmlChar*)"pic_1");
  if(!val)
  {
    return FALSE;
  }	
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);

  wifilayout->bg_wifi->pixbuf1 = sms_load_pic(pic,wifilayout->bg_wifi->bg_w1,wifilayout->bg_wifi->bg_h1);
  if(!wifilayout->bg_wifi->pixbuf1)
  {
    return FALSE;
  }	
  wifilayout->bg_wifi->pic1 = 
    goo_canvas_image_new(layout->cavas ,wifilayout->bg_wifi->pixbuf1 ,wifilayout->bg_wifi->bg_x1 ,wifilayout->bg_wifi->bg_y1 ,NULL);
  g_object_set(wifilayout->bg_wifi->pic1,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_unref(wifilayout->bg_wifi->pixbuf1);


	val = xmlGetProp (a_node,(xmlChar*)"pic_2");
  if(!val)
  {
    return FALSE;
  }	
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  
  wifilayout->bg_wifi->pixbuf2 = sms_load_pic(pic,wifilayout->bg_wifi->bg_w2,wifilayout->bg_wifi->bg_h2);
  if(!wifilayout->bg_wifi->pixbuf2)
  {
    return FALSE;
  }	
  wifilayout->bg_wifi->pic2 = 
    goo_canvas_image_new(layout->cavas ,wifilayout->bg_wifi->pixbuf2 ,wifilayout->bg_wifi->bg_x2 ,wifilayout->bg_wifi->bg_y2 ,NULL);
  g_object_set(wifilayout->bg_wifi->pic2,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_unref(wifilayout->bg_wifi->pixbuf2);


  return TRUE;
}

static void  wifi_setting_layout_create_icon(xmlNode *a_node,Layout *layout ,int id)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  SMS *sms=layout->sms;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  printf("id=%d\n" ,id);
  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
  {
    goto err;
  }

  wifilayout->icons[id].name=g_strdup(val);
  wifilayout->icons[id].size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
  wifilayout->icons[id].asize=sms_get_xml_int(a_node,(xmlChar*)"asize",wifilayout->icons[id].size);     
  wifilayout->icons[id].x=sms_get_xml_int(a_node,(xmlChar*)"x",wifilayout->icons[id].x);     
  wifilayout->icons[id].y=sms_get_xml_int(a_node,(xmlChar*)"y",wifilayout->icons[id].y);   

  /************icon creat****************/
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    goto err;
  }


  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,wifilayout->icons[id].size,wifilayout->icons[id].size);
  if(!pixbuf)
  {
    goto err;
  }

  wifilayout->icons[id].icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);	
  g_object_set(wifilayout->icons[id].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  /*************icon ani******************/
  val = xmlGetProp (a_node,(xmlChar*)"apic");
  if(!val)
  {
    goto err;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,wifilayout->icons[id].asize,wifilayout->icons[id].asize);
  if(!pixbuf)
  {
    goto err;
  }

  wifilayout->icons[id].icon_a=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0, NULL);
  g_object_set(wifilayout->icons[id].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  return ;

err:
  printf("fdfdfdf\n");
  fprintf(stderr,"%s:error\n",__FUNCTION__);

}

static gboolean wifi_setting_layout_load_icons(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  xmlNode *cur_node = NULL;
  int i=0;
  Icon_Wifi* icon_wifi;

  if(!a_node)
  {
    return FALSE;
  }

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      if(strcmp(cur_node->name,"icon")==0){
        wifi_setting_layout_create_icon(cur_node,layout ,i);
        i++;
      }
    }
  }
  printf("i = %d \n" ,i);
  wifilayout->micon_count = i ;

  return TRUE ;
}

gboolean my_wifi_init(Layout *layout) 
{
	WifiLayout *wifilayout = layout->private ;
	wifilayout->wifi = (NetWorkWifi *)g_malloc0(sizeof(NetWorkWifi)+1);
	wifilayout->wifi->my_wifi = (MY_WIFI *)g_malloc0(sizeof(MY_WIFI)+1);

	if (wifilayout->wifi->my_wifi)
	{
        wifilayout->wifi->my_wifi->cell_index=-1;
        wifilayout->wifi->my_wifi->interface_index=-1;        
        //wifi->my_wifi = my_wifi;
        
        memset(wifilayout->wifi->Link_AP_MAC, 0, 48);
        wifilayout->wifi->global_build_list = NULL;
        return TRUE;
	}
	return FALSE ;
}

static void arrange_tree_view(GtkTreeView *treeview)
{
    GtkCellRenderer *render;
    GtkTreeViewColumn *column;

    // id invisible
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("ESSID", render, "text", COL_ESSID, NULL);
    gtk_tree_view_append_column(treeview, column);

    // playURL
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("Mode", render, "text", COL_MODE, NULL); 
    gtk_tree_view_append_column (treeview, column);
    gtk_tree_view_column_set_visible(column, FALSE);

    // icon
    render = gtk_cell_renderer_text_new();
#ifdef WEP_CHECK
    column = gtk_tree_view_column_new_with_attributes ("Type", render, "text", COL_KEY, NULL);
#else
    column = gtk_tree_view_column_new_with_attributes ("KEY", render, "text", COL_KEY, NULL);
#endif
    gtk_tree_view_append_column(treeview, column);

    // title
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("Signal", render, "text", COL_QUALITY, NULL);
    gtk_tree_view_append_column(treeview, column);  

    // type(video or list)
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes ("State", render, "text", COL_STATE, NULL); 
    gtk_tree_view_append_column(treeview, column);
}

static void read_wifi_passwd(GtkWidget *entry)
{
	printf("~~~%s\n" ,__FUNCTION__);
	FILE *fd;
	gchar buf[64];
	memset(buf ,0 ,sizeof(buf));

	fd = fopen("/etc/network/passwd_wifi" ,"r+");
	if (fd)
	{
		fgets(buf ,64 ,fd);
		printf("!!!!!!!!!!!!!buf = %s \n" ,buf);
	}
	gtk_entry_set_text(GTK_ENTRY(entry) , buf);
	fclose(fd);
}

static void write_wifi_passwd(Layout *layout)
{
	printf("~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	FILE *fd;

	fd = fopen("/etc/network/passwd_wifi" ,"r+");
	if (fd)
	{
		fprintf(fd, "%s", wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->userkey);
	}
	fclose(fd);
}

gboolean get_interface(Layout *layout)
{
	printf("~~~%s\n" ,__FUNCTION__);
		WifiLayout *wifilayout = layout->private ;
#ifdef WEP_CHECK
    FILE *fd;
	gchar buffer[256];
    
    wifilayout->wifi->my_wifi->interface_count = 0 ;

	fd = fopen("/proc/net/dev", "r");

	while(fgets(buffer, 255, fd))
	{
		if(g_strrstr(buffer, "eth1"))
		{
			if(wifilayout->wifi->my_wifi->wifi_interface[0]==NULL)
               			 wifilayout->wifi->my_wifi->wifi_interface[0]=(WIFI_INTERFACE *)g_malloc0(sizeof(WIFI_INTERFACE)+1);
			strncpy(wifilayout->wifi->my_wifi->wifi_interface[0]->name,"eth1",sizeof(WIFI_INTERFACE));
			wifilayout->wifi->my_wifi->interface_count++;
			printf("interface name = %s\n", wifilayout->wifi->my_wifi->wifi_interface[0]->name);
			return TRUE;
		}
	}
	fclose(fd);
	return FALSE;
#else
    FILE *pipe;
    gchar *cmd;
    gchar *search_prefix;
    gchar **tmpline;
    gchar buffer[512];
    gchar *tmp;
    gint index;


    //printf("cell cnt = %d, interface cnt = %d\n", wifi->my_wifi->cell_count, wifi->my_wifi->interface_count);
		printf("get interface start!!!\n");

    cmd=g_strdup_printf("cat /proc/net/wireless");	
    search_prefix=g_strdup(" face");
    pipe = popen (cmd, "r");
    if(!pipe){perror("pipe:");return FALSE;}
    wifilayout->wifi->my_wifi->interface_count=0;
    wifilayout->wifi->my_wifi->interface_index=0;
    index=0;
    if(pipe){
        while ((feof(pipe) == 0)){
             fgets (buffer, 255, pipe);             
             //printf("get buf: %s \n", buffer);
             if (g_str_has_prefix(buffer,search_prefix)){
                 while (feof(pipe)==0){ 
                    printf("%s \n", __FUNCTION__);
                    fgets(buffer,255, pipe);
                    tmpline=g_strsplit(buffer,":",512);
                    tmp=g_strstrip(tmpline[0]);
		    if(wifilayout->wifi->my_wifi->wifi_interface[index]==NULL)
                    wifilayout->wifi->my_wifi->wifi_interface[index]=(WIFI_INTERFACE *)g_malloc0(sizeof(WIFI_INTERFACE));
                    //memset(my_wifi->wifi_interface[index],0,sizeof(WIFI_INTERFACE));
                    strcpy(wifilayout->wifi->my_wifi->wifi_interface[index]->name,tmp);
                    index++;
                    wifilayout->wifi->my_wifi->interface_count++;
                 }                 
             }
        }
        wifilayout->wifi->my_wifi->interface_count--;
    }
    pclose(pipe);
    return FALSE;
#endif
}

static gboolean wifi_disconnect(GtkMenuItem *menuitem, Layout *layout)
{
		WifiLayout *wifilayout = layout->private ;
    GtkWidget *button;
    char buffer[128];
    gchar *cmd;
    
    printf("this cell is connect, call %s!\n", __func__);

    //cmd=g_strdup_printf("/sbin/xip-ifdown %s", wifi->my_wifi->wifi_interface[0]->name);
    cmd=g_strdup_printf("/bin/net-disconnect %s", wifilayout->wifi->my_wifi->wifi_interface[0]->name);
    system(cmd);
    printf("this cell is connect, cmd: %s!\n", cmd);
    g_free(cmd);
    
    sprintf(buffer, "Disconnect");
    review_tree_vanpro(buffer, layout);
    if (wifilayout->wifi->connect_index != -1)
        wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate=LINK_DOWN;
    
    wifilayout->wifi->connect_index=-1;
    return FALSE;
}

gboolean do_disconnect(Layout *layout)
{
		return wifi_disconnect(NULL, layout);
}

static void wirte_interface_info(WIFI_INTERFACE *iface)
{
		printf("~~~%s\n" ,__FUNCTION__);
    FILE *fd = NULL;
    gchar *ret = NULL;
    gchar *filename = NULL;
    gchar ifname[256] = {0};
    gchar buffer[256] = {0};
    gchar paramval[256] = {0};

    // check param
    if (NULL == iface)
    {
        fprintf(stderr, "input param invalid in %s\n", __func__);
        return;
    }

    filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);    
    remove(filename);
    fd = fopen(filename, "w");
    if (fd)
    {
        if (iface->name)
        {
            fprintf(fd, "iface %s\n", iface->name);
        }      
        if (iface->isstatic)
        {
            fprintf(fd, "addrtype static\n");
        }
        else
        {
            fprintf(fd, "addrtype dhcp\n");
        }        
        if (iface->address)
        {
            fprintf(fd, "address %s\n", iface->address);
        }
        if (iface->netmask)
        {
            fprintf(fd, "netmask %s\n", iface->netmask);
        }
        if (iface->gateway)
        {
            fprintf(fd, "gateway %s\n", iface->gateway);
        }
        if (iface->nameserver[0])
        {
            fprintf(fd, "nameserver %s\n", iface->nameserver[0]);
        }
        else
        {
            fprintf(fd, "nameserver \n");
        }
        if (iface->nameserver[1])
        {
            fprintf(fd, "nameserver %s\n", iface->nameserver[1]);
        }
    }
    fclose(fd);
}

static gint ip_esc_press(GtkWidget *widget, GdkEventButton *event, Layout *layout)
{
		GdkEventKey *key;
		WifiLayout *wifilayout = layout->private ;

    key = (GdkEventKey *)event;
    if (key->keyval != GDK_Escape)
    {
        return FALSE;
    }

    wifilayout->wifi->my_wifi->wifi_interface[0]->isstatic = FALSE;
    wifilayout->wifi->my_wifi->wifi_interface[0]->isdhcp = TRUE;
    wirte_interface_info(wifilayout->wifi->my_wifi->wifi_interface[0]);
    gtk_widget_destroy(widget);
    return TRUE;
}

static GtkWidget* create_ipconfig_dlg(Layout *layout)
{
    gint result;
    GtkWidget *msgdlg;
		WifiLayout *wifilayout = layout->private ;
    
    // create a message dialog
    msgdlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_NONE,
        "Please select a lan connect mode, use dhcp get ip automatically or set static ip manually.");
		g_signal_connect(G_OBJECT(msgdlg), "delete-event",
                	G_CALLBACK(gtk_widget_destroy), NULL);

    gtk_dialog_add_buttons(GTK_DIALOG(msgdlg), "DHCP", RESPONSE_DHCP,
                                   "Static IP", RESPONSE_STATIC, NULL);
    gtk_widget_add_events(GTK_WIDGET(msgdlg), GDK_KEY_PRESS_MASK);
    gtk_signal_connect(GTK_OBJECT(msgdlg), "key-press-event",GTK_SIGNAL_FUNC(ip_esc_press),layout);
    return msgdlg;
}

static gboolean address_is_valid(gchar *ip)
{
    if(ip==NULL)return FALSE;
    struct sockaddr_in addr;
    if (inet_pton(AF_INET, ip, &addr.sin_addr)==0){
        perror("inet_pton");
        printf("invalid: (%s), len: (%d)\n", ip, strlen(ip));
        return FALSE;
    }
    return TRUE;
}

static void save_nameserver(Layout *layout)
{
    gchar *name1 = NULL;
    gchar *name2 = NULL;
    gboolean changed = FALSE;
    GtkWidget *entry;
    WifiLayout *wifilayout = layout->private ;

    
	// save nameserver
    FILE *fd = fopen("/etc/resolv.conf", "w");     
    if (!fd)
    {
        printf("file /etc/resolv.conf cann't access\n");
    }

    if (wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0])
    {
        fprintf(fd, "nameserver %s\n", wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0]);
    }
    if (wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1])
    {
        fprintf(fd, "nameserver %s\n", wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1]);
    }

    fclose(fd);
    return;
}

static void wifi_set_staticip(Layout *layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
		GtkWidget *entry;
    gchar *name1 = NULL;
    gchar *name2 = NULL;
		gchar *newval = NULL;
    gchar *ipinfo = NULL;
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;


    if (wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0] != NULL)
    {
        g_free(wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0]);
        wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0] = NULL;
    }
    if (wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1] != NULL)
    {
        g_free(wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1]);
        wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1] = NULL;
    }
    
    wifilayout->wifi->my_wifi->wifi_interface[0]->isstatic = TRUE;
    wifilayout->wifi->my_wifi->wifi_interface[0]->isdhcp = FALSE;
    entry = g_object_get_data(G_OBJECT(sms->window), "Address");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifilayout->wifi->my_wifi->wifi_interface[0]->address, newval);
            printf("@@@address = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->address);
        }
        else
        {
            ipinfo=g_strdup("Input Address is invalid");
            //show_message_box(wifilayout->wifi->mainwindow, ipinfo, FALSE);
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(sms->window), "Netmask");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifilayout->wifi->my_wifi->wifi_interface[0]->netmask, newval);
            printf("@@@netmask = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->netmask);
        }
        else
        {
            ipinfo=g_strdup("Input Netmask is invalid");
            //show_message_box(wifilayout->wifi->mainwindow, ipinfo, FALSE); 
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(sms->window), "Gateway");
    if (entry)
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifilayout->wifi->my_wifi->wifi_interface[0]->gateway, newval);
            printf("@@@gateway = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->gateway);
        }
        else
        {
            ipinfo=g_strdup("Input Gateway is invalid");
            //show_message_box(wifilayout->wifi->mainwindow, ipinfo, FALSE);
            return;
        }
    }
    entry = g_object_get_data(G_OBJECT(sms->window), "Primary DNS server");
    if (entry)
    {
        name1 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("@@@@name = %s %d\n" ,name1 ,strlen(name1));
        if (strlen(name1) >= 7)
        {
            if (address_is_valid(name1))
            {
                wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0] = g_strdup(name1);
                printf("@@@nameserver0 = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0]);
            }
            else
            {
                printf("show nameserver option invalid 1: %s, len = %d\n", name1, strlen(name1));
                //show_message_box(sms->window, _("nameserver option invalid"), FALSE);  
                return;
            }
        }
    }

    entry = g_object_get_data(G_OBJECT(sms->window), "Secondary DNS server");
    if (entry)
    {
        name2 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("@@@@name = %s %d\n" ,name2 ,strlen(name2));
        if (strlen(name2)>=7)
        {
            if (address_is_valid(name2))
            {
                wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1] = g_strdup(name2);
                printf("@@@nameserver1 = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1]);
            }
            else
            {
                printf("show nameserver option invalid 2: %s, len = %d\n", name2, strlen(name2));
                //show_message_box(sms->window, _("nameserver option invalid"), FALSE);        
                return;
            }
        }
    }

    save_nameserver(layout);
    printf("wirte_interface_info, dhcp: %d\n", wifilayout->wifi->my_wifi->wifi_interface[0]->isdhcp);
    wirte_interface_info(wifilayout->wifi->my_wifi->wifi_interface[0]);
    //printf("write interface info: isdhcp: %d, ip: %s, netmask: %s, gateway: %s, nameserver1: %s, nameserver2: %s\n", 
    //        lan->iface.isdhcp, lan->iface.address, lan->iface.netmask, lan->iface.gateway, lan->iface.nameserver[1], lan->iface.nameserver[2]);
}


gboolean address_valid(WIFI_INTERFACE *iface)
{
    if(!iface)
        return FALSE;
    int inet_sock;
    struct ifreq ifr;
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(ifr.ifr_name, iface->name);
    if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0){
        close(inet_sock);
        return FALSE;
    }else{
        close(inet_sock);
        return TRUE;
    }
}

gboolean detect_network_interface_state(Layout *layout)
{
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ; 

    gchar buffer[128];
    GtkWidget *button;
    static int count=0;
    printf("detect_network_interface_state ...\n");
    

    if(address_valid(wifilayout->wifi->my_wifi->wifi_interface[0])){//if address is valid
        count=0;
        printf("acquire address successed.\n");
        //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS",WIFI_STATUS_GETIP);
        sprintf(buffer, "Wireless network selection");        
        set_title_label(buffer, layout);
        get_interface_statistics_data(wifilayout->wifi->my_wifi->wifi_interface[0]);
    #if 0
        sprintf(buffer,"%s:%s",_("Connected\nIP Address"),wifi->my_wifi->wifi_interface[0]->address);
        review_tree_vanpro(buffer, wifi);
    #else
        sprintf(buffer,"%s:%s","Connected.\nIP Address",wifilayout->wifi->my_wifi->wifi_interface[0]->address);
        //show_connect_info(buffer, layout);
        wifilayout->wifi->RUNNING = FALSE;
    #endif
        return FALSE;
    }else{//if address is invalid
        count++;
        if(count>20){
            count=0;
			if(wifilayout->wifi->my_wifi->wifi_interface[0]->wifi_group_enable)
				return FALSE;  
            printf("acquire address failed.\n");
            sprintf(buffer, "Invalid IP");
            review_tree_vanpro(buffer, layout);   
            //show_connect_info(buffer, layout);
            wifilayout->wifi->RUNNING = FALSE;
            return FALSE;            
        }
    }
    return TRUE;
}

gboolean detect_network_link_state(Layout *layout)
{
		printf("~~%s\n" ,__FUNCTION__);
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;   
    
    static int link_count=0;
    
    char buffer[128];
    get_ap_mac(layout);
    if(g_str_equal(wifilayout->wifi->Link_AP_MAC,"")||g_str_equal(wifilayout->wifi->Link_AP_MAC,"00:00:00:00:00:00")){
        printf("AP MAC[%s]:Disconnected\n",wifilayout->wifi->Link_AP_MAC);
        link_count++;
        if(link_count>20){		//link_connect count more than 20
            wifilayout->wifi->linkconnect=FALSE;            
            //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS",WIFI_STATUS_UP);
            printf("Not connect,return.\n");
            //sprintf(buffer, _("Not Connect"));
            sprintf(buffer, "Disconnect");
            review_tree_vanpro(buffer, layout);     
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate=LINK_DOWN;
            sprintf(buffer, ("Wireless network selection"));
            set_title_label(buffer, layout);
            link_count=0;
            //show_connect_info(_("Not Connect."), layout);
            wifilayout->wifi->RUNNING = FALSE;
            //doubleclick = TRUE ;
            return FALSE;
        }
        return TRUE;
        
    }else{
        wifilayout->wifi->linkconnect=TRUE;
        //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS", WIFI_STATUS_CONNECT);
        printf("link connected,then we will require ip address.\n"); 
        sprintf(buffer, "Connected");
        review_tree_vanpro(buffer, layout);
        memset(buffer, 0, 128);
        //show_connect_info("Connected.\nAcquire IP address...", layout);
        wifilayout->wifi->RUNNING = TRUE;
        //require_ip_address(wifi);
        g_timeout_add(2000, (GSourceFunc)detect_network_interface_state,layout);
        link_count=0;        
        return FALSE;
    }
}

void save_config_to_file(gchar *iface, gchar *config)
{
    FILE *fd;
    FILE *fp;

    if (config == NULL)
    {
        return;
    }
    
    remove(WIRELESS_LAST_CONF);
    remove(NETWORK_CONFIG);
    fd = fopen(WIRELESS_LAST_CONF, "w");
    if (fd == NULL)
    {
        printf("open file: %s failed\n", WIRELESS_LAST_CONF);
        return;
    }
    fprintf(fd, "%s\n", config);   
    fclose(fd);

    // open interface
    if ((fp = fopen(NETWORK_CONFIG, "w+")) == NULL)
    {
        printf("cannot open file: %s!\n", NETWORK_CONFIG);
        return;
    }
    fputs(iface, fp);
    fclose(fp);
}

void apply_configuration(Layout *layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;   
    gchar *cmd;
    gchar *s_enc;
    gchar *s_key;
			gchar *s_mode;
    char essid_buf[256];
    

    if(wifilayout->wifi->my_wifi->interface_count>0){
		
        //printf("essid to set:%s\n",wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->essid);
        printf("interface:%s\n",wifilayout->wifi->my_wifi->wifi_interface[0]->name);
        
        if(g_str_equal(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->userkey, "")){
            s_key=g_strdup("off");
        }else{
            if(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->userkey)
                s_key=g_strdup(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->userkey);
            else
                s_key=g_strdup("");            
        }
        
        
        if(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->encmode==ENC_OFF){
            s_enc=g_strdup("off");
        }else if(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->encmode==ENC_OPEN){
            s_enc=g_strdup("open");            
        }else{
            s_enc=g_strdup("restricted");
        }        
	
        if(strcasecmp(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->mode,"Master")==0)
    	{
    		s_mode=g_strdup("Managed");
    	}
	else
		s_mode=g_strdup(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->mode);
    #if 1
        if(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->key_index == 0)
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->key_index = 1;
    #endif
        //if(!g_str_has_prefix(wifi->type, "WEP"))
        if(g_str_has_prefix(wifilayout->wifi->type, "WPA"))
        {
#ifdef WEP_CHECK
            cmd=g_strdup_printf("wpa \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" dev \"%s\"", 
                    wifilayout->wifi->my_wifi->wifi_interface[0]->name,
                    wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->essid,
                    s_mode,    
                    s_key,
                    s_enc,
                    wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->device_type);
#else
                cmd=g_strdup_printf("wpa \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\"", 
                        wifilayout->wifi->my_wifi->wifi_interface[0]->name,
                        wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->essid,
                        s_mode,    
                        s_key,
                        s_enc);
#endif
                printf("wpa_supplicant cmd=%s\r\n",cmd);
		}
		else
        {
			//cmd=g_strdup_printf("wep \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" \"[%d]\"", 
            cmd=g_strdup_printf("wep \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" [%d]", 
					wifilayout->wifi->my_wifi->wifi_interface[0]->name,
					wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->essid,
					s_mode,
					s_key,
					s_enc,
                    wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->key_index);//wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index+1
			printf("iwset cmd=%s\r\n",cmd);
		}
        save_config_to_file(wifilayout->wifi->my_wifi->wifi_interface[0]->name, cmd);
        printf("------save wifi config: %s\n", cmd);
        g_free(cmd);
        //cmd = g_strdup_printf("%s restart", NETWORK_SETUP_SCRIPT);
        cmd = g_strdup_printf("%s connect", NETWORK_SETUP_SCRIPT);
        system(cmd);

        g_free(cmd);
		g_free(s_mode);
		g_free(s_key);
		g_free(s_enc);
    }
}

gboolean wifi_do_connect(Layout *layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
    apply_configuration(layout);
    g_timeout_add(2000,(GSourceFunc)detect_network_link_state,layout);
    return FALSE;
}

static void connet(GtkWidget *widget, Layout *layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

   		GtkWidget *dlg;
    GtkWidget *entry;
    gchar *newval;
    char buffer[64];
    gint index = 0;

		entry=g_object_get_data(G_OBJECT(sms->window), "key_index");
		printf("entry key_index = %p\n" ,entry);
    if (entry) 
    {   
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        index = atoi(newval);
        if ((index <= 4) && (index >= 1))
        {
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->key_index = index;
        }
        else
        {
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->key_index = 1;
        }
    }

    entry=g_object_get_data(G_OBJECT(sms->window), "PassWord");
    printf("entry passwd= %p\n" ,entry);
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("~~~~~~passwd = %s\n" ,newval);
        
        {
	        strcpy(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->userkey, newval);
        }
        write_wifi_passwd(layout );
    }
    wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->encmode=ENC_OPEN;


    wifilayout->wifi->RUNNING = TRUE;			//wifi run flags
    g_timeout_add(100, (GSourceFunc)wifi_do_connect, layout);
}

static gint get_first_char_wifi(gchar *s)
{
	gint i = 0;

    if (s == NULL)
    {
        return -1;
    }
	for (i=0; i<strlen(s); i++)
		if ((s[i] != ' ') && (s[i] != '\t') && (s[i] != '\n') && (s[i] != '\0')) return i;
	return -1;
}

static gint get_param_val_wifi(gchar* line, gchar* param, gchar* value)
{
		printf("~~~%s\n" ,__FUNCTION__);
    gint a = 0;
    gint b = 0;
    gint st = 0;
    gint sep = 0;
	
    st = get_first_char_wifi(line);
    if (line[st] == '#') return -1; // mark comment 
    for (sep=st; sep<strlen(line); sep++)
        if (line[sep]==' ') break;
    for (a=sep; a<strlen(line); a++)
        if ((line[a]!=' ') && (line[a]!='\t')) break;
    for (b=a+1; b<strlen(line); b++)
        if ((line[b]=='\n') || (line[b]=='\0')) break;
    param = strncpy(param,&line[st],sep-st);
    param[sep-st] = '\0';

    if ((b-a) > 0) value = strncpy(value,&line[a],b-a);	
    value[b-a]='\0';
    return 0;
}

static void read_interface_info(WIFI_INTERFACE *iface)
{
		printf("~~~%s\n" ,__FUNCTION__);
    gchar *filename;
    FILE *fd;
    char buffer[256];
    gchar paramval[256];
    gchar ifname[256];
    gchar *ret;
    gint index = 0;

		printf("22222222222222222222paramval = %s\n" ,iface->name);
    filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);   
    printf("22222222222222222222paramval = %s\n" ,filename);
    if (!access(filename, 4))
    {
        printf("Access file success:%s\n", filename);
        fd = fopen(filename, "r");
        if (fd)
        {
            do
            {
                ret = fgets(buffer, 255, fd);
                if (get_param_val_wifi(buffer, paramval, ifname) == -1) 
                {
                    printf("call %s failed!\n");
                    memset(buffer, 0, 256);
                    continue;  
                } 
                
                if (!strcmp("iface", paramval))
                {
                		printf("iface =%s \n" ,paramval);
                    if (!strcmp(paramval, iface->name))
                    {
                        printf("Worng config file.\n");
                        fclose(fd);
                        return;
                    }
                }
                else if (!strcmp("address", paramval))
                {
                    strcpy(iface->address, g_strstrip(ifname));
                    printf("address =%s \n" ,iface->address);
                }
                else if (!strcmp("netmask", paramval))
                {
                    strcpy(iface->netmask,g_strstrip(ifname));
                    printf("netmask =%s \n" ,iface->netmask);
                }
                else if(!strcmp("gateway", paramval))
                {
                    strcpy(iface->gateway, g_strstrip(ifname));
                    printf("gateway =%s \n" ,iface->gateway);
                }
                else if(!strcmp("nameserver", paramval))
                {
                    iface->nameserver[index] = g_strdup(ifname);
                    index++;
                    printf("nameserver =%s \n" ,iface->nameserver[index]);
                }
                memset(buffer, 0, 256);
            }while(ret!=NULL);
            fclose(fd);
        }
    }
}


void cb_connect(Layout *layout) 
{
    GtkWidget *dialog,*entry,*combobox,*ipdlg;
    gint response;
    gchar *newval;
    char buffer[64];
    gint msgret = 0;
    gint ipret = 0;

    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

    printf("Choice to Connect index[%d],count[%d]\n", wifilayout->wifi->my_wifi->cell_index, wifilayout->wifi->my_wifi->cell_count);
    get_interface(layout);
    /*kine**************set label info****************/
    if(wifilayout->wifi->my_wifi->interface_count<1){// No Device
        sprintf(buffer, "No Wireless Device");
				set_title_label(buffer, layout);
        return;
    }
    
    if(wifilayout->wifi->my_wifi->cell_count<1){ //No Network
    		sprintf(buffer, "No Network in Local Area");
				set_title_label(buffer, layout);
        return;
    }    
    if(wifilayout->wifi->my_wifi->cell_index<0){ //No choice
    		sprintf(buffer, "Select a Network First");
				set_title_label(buffer, layout);
        return;
    }
    wifilayout->wifi->connect_index=wifilayout->wifi->my_wifi->cell_index;

    if(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate==LINK_UP){
        printf("this cell is connect, will be link down!\n");
        g_timeout_add(100,(GSourceFunc)do_disconnect,layout);
        wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate==LINK_DOWN;
        printf("now is link down.\n");  
        return;
    }
}

static void wifi_connect(GtkMenuItem *menuitem, Layout *layout)
{
			printf("~~~%s\n" ,__FUNCTION__);
    GtkWidget *treeview;
    GtkTreeSelection *selection;
    GtkTreePath *path;
    gint *sel_index;
    GtkTreeModel *model;
    GtkTreeIter iter;
    
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

    treeview = g_object_get_data(G_OBJECT(sms->window), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));

    if (!gtk_tree_selection_get_selected(selection, (GtkTreeModel **)&model, &iter))
    {
        printf("there is no selected signal, can't connect!\n");
        return;
    }

    gchar *type = NULL;
    gtk_tree_model_get(model, &iter, COL_KEY, &type, -1);
    if (wifilayout->wifi->type != NULL)
    {
        g_free(wifilayout->wifi->type);
    }
    wifilayout->wifi->type = g_strdup_printf("%s", type);

    path = gtk_tree_model_get_path(model, &iter);
    if (path) 
    {
        sel_index = gtk_tree_path_get_indices(path);
        if (sel_index) 
        {
            wifilayout->wifi->my_wifi->cell_index = *sel_index;
        }
        if (gtk_tree_selection_path_is_selected(selection, path)) 
        {
            gtk_tree_selection_unselect_all(selection);
            gtk_tree_selection_select_path(selection, path);            
        }
        gtk_tree_path_free(path);
    }

    g_return_if_fail(wifilayout->wifi->my_wifi->cell_index>-1);
    g_return_if_fail(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->my_wifi->cell_index] != NULL);   

		printf("connect_index = %d, my_wifi->cell_index=[%d]\n", 
        wifilayout->wifi->connect_index, wifilayout->wifi->my_wifi->cell_index);
		
    if (wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->my_wifi->cell_index]->linkstate != LINK_UP)
    {
        cb_connect(layout);
    }
    return;
}

void cb_view_sel(GtkTreeView *tree_view, GtkTreePath *path,
                    GtkTreeViewColumn *column,
                    Layout *layout)
{
		printf("~~%s\n" ,__FUNCTION__);
		hide_list(layout);
		show_wifi_net_address(layout);
		
		printf("select ^^^^^\n");
		WifiLayout *wifilayout = layout->private ;
		SMS *sms = layout->sms ;
		//sms_change_win_to(layout->sms,LAYOUT_WIN_LAN_SETTING);
    
    wifilayout->wifi->RUNNING = TRUE ;
    printf("cb_view_sel start!!!\n");
    g_return_if_fail(wifilayout->wifi->my_wifi->cell_count>0
                    && wifilayout->wifi->my_wifi->interface_count>0);
    printf("Cell count %d index %d interface count %d\n",
        wifilayout->wifi->my_wifi->cell_count, wifilayout->wifi->my_wifi->cell_index, wifilayout->wifi->my_wifi->interface_count);

    wifi_connect(NULL, layout);
    return ;
}

GtkTreeModel* create_model() 
{
    GtkListStore  *store;
    
    store = gtk_list_store_new(n_cols, G_TYPE_STRING, G_TYPE_STRING,
                                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
    return GTK_TREE_MODEL(store);
}


GtkWidget *create_list(Layout *layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
    GtkWidget *vbox;
    GtkWidget *label;
    GtkWidget *table;    
    GtkWidget *button;
    GtkWidget *scrolled_window;
    GtkWidget *list_scrolled_window;
    GtkWidget *view;

  		WifiLayout *wifilayout = layout->private ;
  		SMS *sms = layout->sms ;

  		vbox = gtk_vbox_new(FALSE, 10);
  		label = gtk_label_new("Press up or down to select a signal, then press OK to connect, or Esc to quit.");
  		gtk_widget_set_name(label, "title-label");
  		gtk_object_set_data(GTK_OBJECT(sms->window), "title-label", label);
  		gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
  		gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 8);

  		view = gtk_tree_view_new();
  		gtk_widget_set_name(view, "view-press");
  		gtk_object_set_data(GTK_OBJECT(sms->window), "view-press", view);

  		// arrange view columns
  		arrange_tree_view(GTK_TREE_VIEW(view));
  		g_signal_connect(G_OBJECT(view), "row-activated", G_CALLBACK(cb_view_sel), layout);

    // set model
    GtkTreeModel* store = create_model();
    g_object_set_data(G_OBJECT(sms->window), "cell-list", store);
    gtk_tree_view_set_model(GTK_TREE_VIEW(view), store);
    g_object_unref(store);    

    list_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(list_scrolled_window),
                                   GTK_POLICY_ALWAYS,
                                   GTK_POLICY_AUTOMATIC);//GTK_POLICY_AUTOMATIC

    //gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(list_scrolled_window), view);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_scrolled_window),
                                         GTK_SHADOW_IN);
    gtk_container_add(GTK_CONTAINER(list_scrolled_window), view);
    gtk_box_pack_start(GTK_BOX(vbox), list_scrolled_window, TRUE, TRUE, 0);

    wifilayout->wifi->my_wifi->wifi_view = view;
    gtk_widget_show_all(vbox);  
    return vbox;
}

static void set_title_label(gchar *text, Layout *layout)
{
	GtkWidget *label;
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;

	label=g_object_get_data(G_OBJECT(sms->window), "title-label");
    if(label)
    {
        gtk_label_set_text(GTK_LABEL(label),text);
    }
}

gchar *get_essid_value(const char *src, const char *target, int unit)
{
        gchar *tmp,*ret;
        gchar **tmpline;
        if(tmp=strstr(src,target))
        {
                tmp+=strlen(target);
                g_strstrip(tmp);
                tmpline=g_strsplit(tmp,"        ",512);
                if(tmpline)
                {
                        if(unit&&tmpline[1])
                        {
                                ret=g_strdup_printf("%s %s",tmpline[0],tmpline[1]);
                        }
                        else
                                ret=g_strdup(tmpline[0]);
                        g_strfreev(tmpline);   
                }

        }
        else
                ret=NULL;
//      return g_strstrip(ret);
        return ret;
}

gchar *get_string_value(const char *src,const char *target,int unit)
{
	gchar *tmp,*ret;
	gchar **tmpline;
	if(tmp=strstr(src,target))
	{
		tmp+=strlen(target);
		g_strstrip(tmp);
		tmpline=g_strsplit(tmp," ",512);
		if(tmpline){			
			if(unit&&tmpline[1])
			{
				ret=g_strdup_printf("%s %s",tmpline[0],tmpline[1]);
			}
			else
				ret=g_strdup(tmpline[0]);
			g_strfreev(tmpline);	
		}
		
	}
	else
		ret=NULL;
// 	return g_strstrip(ret);
	return ret;
}

void get_cell(Layout *layout)
{
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

	FILE *pipe;
	gchar *cmd;
	gchar *search_prefix;
	gchar **tmpline;
	gchar tmpessid[64];
	gchar buffer[512];
	gchar tmpbuffer[128];
	gchar ap_address[32];
	gchar *value;
    gchar *idstart = NULL;
    gchar *idend = NULL;
	gint tmpi;
#ifdef WEP_CHECK
    gchar *keywords[9]={"Address:","ESSID:","Mode:","Frequency:","Quality=","Quality:","Encryption key:","Driver:", "Encryption type:"};
    unsigned char keywords_used[9]; 
#else
	gchar *keywords[7]={"Address:","ESSID:","Mode:","Frequency:","Quality:","Encryption key:","Bit Rates:"};
	unsigned char keywords_used[7]; 
#endif

	printf("get cell start...\n");
    //printf("cell cnt = %d, interface cnt = %d\n", wifi->my_wifi->cell_count, wifi->my_wifi->interface_count);
    WIFI_INTERFACE *iface = wifilayout->wifi->my_wifi->wifi_interface[0];
	cmd=g_strdup_printf("/sbin/ifconfig %s up",iface->name);
	system(cmd);
    g_free(cmd);
	cmd=g_strdup_printf("iwlist %s scan",iface->name);	
	search_prefix=g_strdup("          Cell");
	pipe = popen (cmd, "r");
	if(!pipe)
		{perror("pipe:");return;}
	
	gint k=0,i;
	gint ad=0;
	wifilayout->wifi->my_wifi->cell_count=0;
	memset(buffer,0,512);

    gint num = sizeof(keywords_used);
	if(pipe){
		while(fgets(buffer,512,pipe))
		{
			if(strstr(buffer,"Cell"))
			{
				printf("FIND CELL[%d]\n",k);
				memset(keywords_used,0,num);				
				wifilayout->wifi->my_wifi->wifi_cell[k]=(WIFI_CELL *)g_malloc0(sizeof(WIFI_CELL));  
				memset(wifilayout->wifi->my_wifi->wifi_cell[k],0,sizeof(WIFI_CELL));
				strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->state,"Disconnect");
				wifilayout->wifi->my_wifi->wifi_cell[k]->linkstate=LINK_DOWN;    
				
				do{
					for(i=0;i<num;i++)
					{
						if(keywords_used[i]==1)
							continue;
                        if(i==1)
                            value=get_essid_value(buffer,keywords[1],0);
                        else
                            value=get_string_value(buffer,keywords[i],0);

                        if(value)
						{
							keywords_used[i]=1;
							break;
						}
					}
					if(i<num)
					{
    					switch (i){
    						case 0:
    							strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->address,value);
    							g_free(value);
    							printf("Address:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->address);
    							break;
    						case 1:	
                            #if 0
    							if(value[0]=='"')
    								value[0]=' ';
    							if(value[strlen(value)-1]=='"')                                  
    								value[strlen(value)-1]=' ';                                
    							strcpy(wifi->my_wifi->wifi_cell[k]->essid,g_strstrip(value));
    							g_free(value);
                            #else
                                idstart = strstr(value, "\"");
                                if (idstart != NULL)
                                {
                                    idend = strstr(idstart+1, "\"");
                                    strncpy(wifilayout->wifi->my_wifi->wifi_cell[k]->essid, idstart+1, idend-idstart-1);
                                }
                                else
                                {
                                    strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->essid, value);
                                }
                            #endif
    							printf("ESSID:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->essid);
    							break;
    						case 2:
    							strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->mode,value);
    							g_free(value);
    							printf("Mode:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->mode);
    							break;
    						case 3:
    							strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->channel,value);
    							g_free(value);
    							printf("Frequency:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->channel);
    							break;
    						case 4:	
                            case 5:
    							strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->quality,value);
    							g_free(value);
    							printf("quality:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->quality);
    							break;
    						case 6:
    							strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->key, value);			
    							if(g_str_equal(value, "on"))
    								wifilayout->wifi->my_wifi->wifi_cell[k]->encmode=ENC_OPEN;
    							else if(g_str_equal(value, "off"))
    								wifilayout->wifi->my_wifi->wifi_cell[k]->encmode=ENC_OFF;
    							else
    								wifilayout->wifi->my_wifi->wifi_cell[k]->encmode=ENC_RESTRICTED;
    							g_free(value);
    							printf("key:%d\n",wifilayout->wifi->my_wifi->wifi_cell[k]->encmode);
    							break;
    						case 7:
                            #ifdef WEP_CHECK
                                strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->device_type,value);
                                g_free(value);
                                printf("device type:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->device_type);
                                break;
                            case 8:
                                strcpy(wifilayout->wifi->my_wifi->wifi_cell[k]->encryption_type,value);
                                g_free(value);
                                printf("encryption type:%s\n",wifilayout->wifi->my_wifi->wifi_cell[k]->encryption_type);
                            #endif
    							goto next_cell;
    							break;
    						default:
    							break;
    					}
					}					
				}while(fgets(buffer,512,pipe));
				
next_cell:	
				if(CELL_COUNT==wifilayout->wifi->my_wifi->cell_count)
					break;
				k++;
				wifilayout->wifi->my_wifi->cell_count++;
			}
		}
	}
	pclose(pipe);
    printf("get cell end, cnt = %d!!!\n", wifilayout->wifi->my_wifi->cell_count);
    //printf("cell , interface cnt = %d\n", , wifi->my_wifi->interface_count);
}

static void get_ap_mac(Layout *layout)
{
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;
    int inet_sock;
    struct iwreq iwr;
    

		memset(&iwr, 0, sizeof(iwr));
    memset(wifilayout->wifi->Link_AP_MAC,0,48);  
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(iwr.ifr_name, wifilayout->wifi->my_wifi->wifi_interface[0]->name);
    memset(wifilayout->wifi->Link_AP_MAC, 0, 48);
    if(ioctl(inet_sock, SIOCGIWAP, &iwr) >= 0) 
    {                  
        sprintf(wifilayout->wifi->Link_AP_MAC, "%02X:%02X:%02X:%02X:%02X:%02X",
        (unsigned char)iwr.u.ap_addr.sa_data[0],
        (unsigned char)iwr.u.ap_addr.sa_data[1],
        (unsigned char)iwr.u.ap_addr.sa_data[2],
        (unsigned char)iwr.u.ap_addr.sa_data[3],
        (unsigned char)iwr.u.ap_addr.sa_data[4],
        (unsigned char)iwr.u.ap_addr.sa_data[5]);  
        printf("Detect Linked Acess Pointer:%s\n", wifilayout->wifi->Link_AP_MAC);
    }
}

static void get_interface_statistics_data(WIFI_INTERFACE *iface)
{
    int inet_sock;
    struct ifreq ifr;
    
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(ifr.ifr_name, iface->name);
    if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)
    {
        strcpy(iface->address, "");
        perror("ioctl");
    }
    else{
        strcpy(iface->address, inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
        printf("address:%s\n", iface->address);
    }
    close(inet_sock);
}

void model_data_new(GtkTreeModel* store,
					const gchar* ESSID,const gchar* Mode,const gchar* Key,
					const gchar* Quality,const gchar *State) 
{
    GtkTreeIter iter;

    gtk_list_store_append(GTK_LIST_STORE(store), &iter);
    gtk_list_store_set(GTK_LIST_STORE(store), &iter,
                        COL_ESSID, ESSID,
						COL_MODE, Mode,
                        COL_KEY, Key,
                        COL_QUALITY, Quality,
                        COL_STATE, State,
                        -1);
}

static void review_tree_vanpro(char *data, Layout *layout)
{
    int i;
    GtkWidget *cell_list;
    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

    cell_list=gtk_object_get_data(GTK_OBJECT(sms->window), "cell-list");
    if(cell_list){
        gtk_list_store_clear((GtkListStore *)cell_list);
    }else
        return;

    for(i=0;i<wifilayout->wifi->my_wifi->cell_count;i++){
        strcpy(wifilayout->wifi->my_wifi->wifi_cell[i]->state,"Disconnect");
        wifilayout->wifi->my_wifi->wifi_cell[i]->linkstate=LINK_DOWN;
        if(i==wifilayout->wifi->connect_index){//if wifi_cell[i] has the connect_index hw address
            strcpy(wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->state,data); 
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate=LINK_UP;
        }
        model_data_new((GtkTreeModel* )cell_list,
        wifilayout->wifi->my_wifi->wifi_cell[i]->essid,
		wifilayout->wifi->my_wifi->wifi_cell[i]->mode,
	#ifdef WEP_CHECK
        wifilayout->wifi->my_wifi->wifi_cell[i]->encryption_type,
    #else
        wifilayout->wifi->my_wifi->wifi_cell[i]->key,
    #endif
        wifilayout->wifi->my_wifi->wifi_cell[i]->quality,
        wifilayout->wifi->my_wifi->wifi_cell[i]->state        
        );
    }

    GtkWidget *treeview;
    GtkTreeSelection *selection;
    treeview = g_object_get_data(G_OBJECT(sms->window), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    GtkTreePath *path = gtk_tree_path_new_from_string("0");
    gtk_tree_selection_select_path(selection, path);
}


gboolean cb_refresh(Layout *layout) 
{
		GtkWidget *button;
    gint i;
    char buffer[128];

    WifiLayout *wifilayout = layout->private ;
    SMS *sms = layout->sms ;

    get_cell(layout);     
    button = g_object_get_data(G_OBJECT(sms->window), "view-press");
    if (button) {
        gtk_widget_set_sensitive(button, TRUE);
    }   
    wifilayout->wifi->my_wifi->cell_index=-1;
    if(wifilayout->wifi->my_wifi->cell_count<1){
        //sprintf(buffer, _("No Network"));
        //set_title_label(buffer, wifi);
        set_title_label("No Network", layout);
        sprintf(buffer, "Wireless network selection");
        set_title_label(buffer, layout); 
        return FALSE;
    }    

    get_ap_mac(layout);
    get_interface_statistics_data(wifilayout->wifi->my_wifi->wifi_interface[0]);
    wifilayout->wifi->connect_index=-1;
    memset(buffer,0,128);
    for(i=0;i<wifilayout->wifi->my_wifi->cell_count;i++){
        wifilayout->wifi->my_wifi->wifi_cell[i]->linkstate=LINK_DOWN;
        if(g_str_equal(g_strstrip(wifilayout->wifi->Link_AP_MAC), wifilayout->wifi->my_wifi->wifi_cell[i]->address)){
            printf("Match_AP_MAC:%s,address:%s\n", wifilayout->wifi->Link_AP_MAC, wifilayout->wifi->my_wifi->wifi_cell[i]->address);
            wifilayout->wifi->my_wifi->wifi_cell[i]->linkstate=LINK_UP;            
            wifilayout->wifi->connect_index=i;
            
            if(g_str_equal(wifilayout->wifi->my_wifi->wifi_interface[0]->address, "")){
                //sprintf(buffer, _("Connected\nIP address invalid"));
                sprintf(buffer, "Invalid IP");
                printf("connect_index[%d] BUFFER:%s\n",wifilayout->wifi->connect_index,buffer);
            }else{
                //sprintf(buffer, "%s:%s", _("Connected\nIP Address"), wifi->my_wifi->wifi_interface[0]->address);
                sprintf(buffer, "Connected");
                printf("connect_index[%d] BUFFER:%s, IP: %s\n",wifilayout->wifi->connect_index, buffer, wifilayout->wifi->my_wifi->wifi_interface[0]->address);
            }
            printf("matched connect info:state [%d],essid:[%s]",
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->linkstate,
            wifilayout->wifi->my_wifi->wifi_cell[wifilayout->wifi->connect_index]->essid);            
        }
    }
    if(wifilayout->wifi->connect_index==-1)
        sprintf(buffer, "Disconnect");
    review_tree_vanpro(buffer, layout);
    sprintf(buffer, "Wireless network selection");
    set_title_label(buffer, layout);
//    pixbuf=gdk_pixbuf_new_from_file("/share/pixmaps/pccard-network.png",NULL);
    return FALSE;
}

gboolean cb_refresh_network(GtkWidget *cb_widget, Layout *layout)
{
    GtkWidget *cell_list;
    GtkWidget *button;
	char buffer[128];
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	printf("Searching...\n");

	sprintf(buffer, "Searching...");
	set_title_label(buffer, layout);
	get_interface(layout);

	printf("my_wifi->interface_count = %d\n", wifilayout->wifi->my_wifi->interface_count);
	if (wifilayout->wifi->my_wifi->interface_count<1)
	{
		sprintf(buffer, "No Wireless Device");
		set_title_label(buffer, layout);
		return FALSE;
	}

	if(wifilayout->wifi->my_wifi->wifi_interface[0]->wifi_group_enable)
	{
		GtkWidget *widget;
		widget=gtk_object_get_data(GTK_OBJECT(sms->window), "view-press");
		gtk_widget_set_sensitive(widget, FALSE);
		widget=gtk_object_get_data(GTK_OBJECT(sms->window), "title-label");
		gtk_label_set_label(GTK_LABEL(widget),"Ad-Hoc Host Mode");
		return FALSE;
	}

    //let widget unused
    cell_list=gtk_object_get_data(GTK_OBJECT(sms->window), "cell-list");
    if(cell_list){
        gtk_list_store_clear((GtkListStore *)cell_list);
    }
    
    button = g_object_get_data(G_OBJECT(sms->window), "view-press");
    if (button) {
        gtk_widget_set_sensitive(button, FALSE);
    }    

    g_timeout_add(100, (GSourceFunc)cb_refresh, layout);
    return FALSE;
    
}

gboolean cb_check_wifi(Layout *layout)
{
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;

	GtkWidget *button;
	GtkWidget *proc_win;

    proc_win = g_object_get_data(G_OBJECT(sms->window), "window-proc-win");
	printf("proc_win = %p\n", proc_win);
    
	if (proc_win)
    {
		gtk_widget_hide(proc_win);
		gtk_widget_destroy(GTK_WIDGET(proc_win));
	}
	get_interface(layout);
	if (wifilayout->wifi->my_wifi->interface_count < 1)
	{
			printf("Can not found any WIFI device. Please confirm that you have a WIFI device (either built-in or USB WIFI).");
	}
	else
 {
        cb_refresh_network(NULL, layout);
	}
	return FALSE ;
	
}

void cb_active_device(Layout *layout)
{
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	GtkLabel *text;
	GtkWidget *vbox;

	if (access("/etc/build_in_wifi", F_OK) == 0)
	{
		get_interface(layout);
		if (wifilayout->wifi->my_wifi->interface_count > 0)
		{
					cb_refresh_network(NULL, layout);
					return ;
		}
	}
	else
	{
			cb_refresh_network(NULL, layout);
			return ;
	}

	//active build-in wifi 
	GtkWidget *proc_win = gtk_window_new(GTK_WINDOW_POPUP);
	gtk_widget_set_name(proc_win, "window-proc-win");
	g_object_set_data(G_OBJECT(sms->window), "window-proc-win", proc_win);

	//active inbuilt wifi
	char cmd[128];
#ifdef WEP_CHECK
    sprintf(cmd, "echo 1 > /proc/iwifi");
#else
	sprintf(cmd, "echo 1 > /proc/jz/iwifi");
#endif
	system(cmd);

	vbox = gtk_vbox_new(FALSE, 0);
	text = GTK_LABEL(gtk_label_new("Checking, please wait..."));
	gtk_label_set_justify(text, GTK_JUSTIFY_CENTER);
	gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(text),FALSE,FALSE,0);
	gtk_container_add(GTK_CONTAINER(proc_win), vbox);
	gtk_window_set_position(GTK_WINDOW (proc_win), GTK_WIN_POS_CENTER);

	g_timeout_add(6050, (GSourceFunc)cb_check_wifi, layout);
	gtk_widget_show_all(proc_win);
	while(gtk_events_pending())
	{
		gtk_main_iteration();
    }
	sleep(6);
	
}

static gint connect_win_key_press_event(GtkWidget *widget, GdkEventButton *event, Layout *layout)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	
    if (wifilayout->wifi->RUNNING)
    {
        return TRUE;
    }

    gtk_widget_hide(widget);
    return FALSE;
}


static gboolean wifi_setting_layout_load_tree(Layout *layout)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	
		system("echo 1 > /proc/iwifi && sleep 1");
    if (!my_wifi_init(layout))
    {
        printf("my_wifi_init error!!!\r\n");
        return -1;
    }

   
    wifilayout->list = create_list(layout);
    wifilayout->tree_list=goo_canvas_widget_new(layout->cavas ,wifilayout->list ,75,75 , 665 ,320 ,NULL);
    g_object_set(wifilayout->tree_list , "visibility" , GOO_CANVAS_ITEM_HIDDEN , NULL);

    // connecting info window
    wifilayout->connect_window = gtk_window_new(GTK_WINDOW_POPUP);//GTK_WINDOW_POPUP
    gtk_window_set_type_hint(GTK_WINDOW(wifilayout->connect_window), GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_window_set_decorated(GTK_WINDOW(wifilayout->connect_window), FALSE);
    gtk_widget_add_events(GTK_WIDGET(wifilayout->connect_window), GDK_KEY_PRESS_MASK);
    g_signal_connect(wifilayout->connect_window, "key-press-event", 
    														G_CALLBACK(connect_win_key_press_event), layout);

    GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
    g_object_set_data(G_OBJECT(wifilayout->connect_window), "label_vbox", vbox);
    GtkWidget *label = gtk_label_new("");
    g_object_set_data(G_OBJECT(wifilayout->connect_window), "label", label);
    //gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);
    gtk_container_add(GTK_CONTAINER(vbox), label);
    gtk_container_add(GTK_CONTAINER(wifilayout->connect_window), vbox);
    gtk_window_set_position(GTK_WINDOW (wifilayout->connect_window), GTK_WIN_POS_CENTER);

		//tree view
		GtkWidget *view = gtk_object_get_data(GTK_OBJECT(sms->window), "view-press");
		gtk_window_set_focus(GTK_WINDOW(sms->window), view);
   	g_idle_add((GSourceFunc)cb_active_device, layout);

  return TRUE ;
}

void hide_wifi_entry(Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  //wifi_is_show = LAN_LAYOUT_SHOW;
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
  Entry_Wifi *entry_net = wifilayout->entrys_net ;

  gtk_widget_hide(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  entry_net = entry_net->next ;
  gtk_widget_hide(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  entry_net = entry_net->next ;
  gtk_widget_hide(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  entry_net = entry_net->next ;
  gtk_widget_hide(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  entry_net = entry_net->next ;
  gtk_widget_hide(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  Entry_Wifi *entry_passwd = wifilayout->entrys_passwd;
  gtk_widget_hide(entry_passwd->entry_wifi);
  g_object_set(entry_passwd->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  entry_passwd = entry_passwd->next ;
  gtk_widget_hide(entry_passwd->entry_wifi);
  g_object_set(entry_passwd->text,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);  
}

void show_wifi_entry(Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  //wifi_is_show = LAN_LAYOUT_SHOW;
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
  Entry_Wifi *entry_net = wifilayout->entrys_net ;

  gtk_widget_show(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_net->entry_wifi), 
  																		wifilayout->wifi->my_wifi->wifi_interface[0]->address);
  printf("address = %s\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]->address);

  entry_net = entry_net->next ;
  gtk_widget_show(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_net->entry_wifi), 
  																		wifilayout->wifi->my_wifi->wifi_interface[0]->netmask);

  entry_net = entry_net->next ;
  gtk_widget_show(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_net->entry_wifi), 
  																		wifilayout->wifi->my_wifi->wifi_interface[0]->gateway);

  entry_net = entry_net->next ;
  gtk_widget_show(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_net->entry_wifi), 
  																		wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[0]);

  entry_net = entry_net->next ;
  gtk_widget_show(entry_net->entry_wifi);
  g_object_set(entry_net->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_net->entry_wifi), 
  																		wifilayout->wifi->my_wifi->wifi_interface[0]->nameserver[1]);

  Entry_Wifi *entry_passwd = wifilayout->entrys_passwd;
  gtk_widget_show(entry_passwd->entry_wifi);
  g_object_set(entry_passwd->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_visibility(GTK_ENTRY(entry_passwd->entry_wifi) ,FALSE);
  read_wifi_passwd(entry_passwd->entry_wifi);

  entry_passwd = entry_passwd->next ;
  gtk_widget_show(entry_passwd->entry_wifi);
  g_object_set(entry_passwd->text,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
  gtk_entry_set_text(GTK_ENTRY(entry_passwd->entry_wifi) , "1");
#ifdef WEP_CHECK
    if(g_str_has_prefix(wifilayout->wifi->type, "WEP"))
    {
        gtk_widget_set_sensitive(entry_passwd->entry_wifi, TRUE);
        g_signal_connect(G_OBJECT(entry_passwd->entry_wifi), "activate",
                         G_CALLBACK(connet), layout);
    }
    else
    {
        gtk_widget_set_sensitive(entry_passwd->entry_wifi, FALSE);
    }
#endif
}

static Entry_Wifi *wifi_create_editable_entry_from_xml(xmlNode *a_node ,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
  Entry_Wifi *entry_wifi = (Entry_Wifi *)g_malloc0 (sizeof(Entry_Wifi)+1);
  GtkTooltips *tooltips ;
  xmlChar * val;

  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
    goto err;
  entry_wifi->name = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"tooltips");
  if(!val)
    goto err;
  entry_wifi->tooltips = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"font");
  if(!val)
    goto err;
  entry_wifi->font = g_strdup(val); 

  entry_wifi->entry_x = sms_get_xml_int(a_node,(xmlChar*)"entry_x",entry_wifi->entry_x);  
  entry_wifi->entry_y = sms_get_xml_int(a_node,(xmlChar*)"entry_y",entry_wifi->entry_y);  
  entry_wifi->text_x = sms_get_xml_int(a_node,(xmlChar*)"text_x",entry_wifi->text_x);  
  entry_wifi->text_y = sms_get_xml_int(a_node,(xmlChar*)"text_y",entry_wifi->text_y);  
  printf("entry_x =%d ,entry_y=%d ,text_x=%d ,text_y=%d\n" ,
      entry_wifi->entry_x , entry_wifi->entry_y ,
      entry_wifi->text_x ,entry_wifi->text_y);

  entry_wifi->text = goo_canvas_text_new(layout->cavas ,entry_wifi->name ,
      entry_wifi->text_x ,entry_wifi->text_y ,-1 ,
      GTK_ANCHOR_W ,"font" ,entry_wifi->font ,NULL) ;
  g_object_set (entry_wifi->text ,"fill-color" ,"black" ,NULL);

  tooltips = gtk_tooltips_new();
  entry_wifi->entry_wifi= gtk_entry_new();
  printf("@@@%p\n" ,entry_wifi->entry_wifi);
  gtk_editable_set_editable(GTK_EDITABLE(entry_wifi->entry_wifi) ,TRUE);
  gtk_tooltips_set_tip(tooltips, entry_wifi->entry_wifi, entry_wifi->tooltips, NULL);
  printf("entry->name = %s\n" ,entry_wifi->name);
  g_object_set_data(G_OBJECT(sms->window), entry_wifi->name, entry_wifi->entry_wifi);
  goo_canvas_widget_new(layout->cavas ,entry_wifi->entry_wifi ,
      entry_wifi->entry_x ,entry_wifi->entry_y ,
      -1 ,-1 ,NULL);

  

  return entry_wifi;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //lan_entry_destroy(lanlayout->entry);
  return ;
  
}

static gboolean wifi_setting_layout_load_entry_net_address(xmlNode *a_node ,Layout *layout)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;

  xmlNode *cur_node = NULL;
  Entry_Wifi *entry_wifi ;
  int i = 0;
  if(!a_node)
    return FALSE;



  for (cur_node = a_node; cur_node; cur_node = cur_node->next)
  {
    if (cur_node->type == XML_ELEMENT_NODE)
    {
      if(strcmp(cur_node->name,"entry")==0)
      {
        entry_wifi = wifi_create_editable_entry_from_xml(cur_node , layout);
        if(entry_wifi)
        {
          wifilayout->entry_count++ ;
          if(wifilayout->entrys_net)
          {
            Entry_Wifi *pentry ;
            pentry = wifilayout->entrys_net->prv ;
            pentry->next = entry_wifi ;
            entry_wifi->prv = pentry ;

            wifilayout->entrys_net->prv = entry_wifi ;
            entry_wifi->next = wifilayout->entrys_net ;
          }
          else
          {
            wifilayout->entrys_net = entry_wifi ;
            entry_wifi->next = entry_wifi ;
            entry_wifi->prv = entry_wifi ;
          }
        }
      }
    }
    //i++;
  }

  return TRUE ;
}


static Entry_Wifi *wifi_create_entry_passwd_from_xml(xmlNode *a_node ,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
  Entry_Wifi *entry_wifi = (Entry_Wifi *)g_malloc0 (sizeof(Entry_Wifi)+1);
  GtkTooltips *tooltips ;
  xmlChar * val;

  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
    goto err;
  entry_wifi->name = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"tooltips");
  if(!val)
    goto err;
  entry_wifi->tooltips = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"font");
  if(!val)
    goto err;
  entry_wifi->font = g_strdup(val); 

  entry_wifi->entry_x = sms_get_xml_int(a_node,(xmlChar*)"entry_x",entry_wifi->entry_x);  
  entry_wifi->entry_y = sms_get_xml_int(a_node,(xmlChar*)"entry_y",entry_wifi->entry_y);  
  entry_wifi->text_x = sms_get_xml_int(a_node,(xmlChar*)"text_x",entry_wifi->text_x);  
  entry_wifi->text_y = sms_get_xml_int(a_node,(xmlChar*)"text_y",entry_wifi->text_y);  
  printf("entry_x =%d ,entry_y=%d ,text_x=%d ,text_y=%d\n" ,
      entry_wifi->entry_x , entry_wifi->entry_y ,
      entry_wifi->text_x ,entry_wifi->text_y);

  entry_wifi->text = goo_canvas_text_new(layout->cavas ,entry_wifi->name ,
      entry_wifi->text_x ,entry_wifi->text_y ,-1 ,
      GTK_ANCHOR_W ,"font" ,entry_wifi->font ,NULL) ;
  g_object_set (entry_wifi->text ,"fill-color" ,"black" ,NULL);

  tooltips = gtk_tooltips_new();
  entry_wifi->entry_wifi= gtk_entry_new();
  printf("@@@%p\n" ,entry_wifi->entry_wifi);
  gtk_editable_set_editable(GTK_EDITABLE(entry_wifi->entry_wifi) ,TRUE);
  gtk_tooltips_set_tip(tooltips, entry_wifi->entry_wifi, entry_wifi->tooltips, NULL);
  printf("entry->name = %s\n" ,entry_wifi->name);
  g_object_set_data(G_OBJECT(sms->window), entry_wifi->name, entry_wifi->entry_wifi);
  goo_canvas_widget_new(layout->cavas ,entry_wifi->entry_wifi ,
      entry_wifi->entry_x , entry_wifi->entry_y ,
      -1 ,-1 ,NULL);

  return entry_wifi ;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //lan_entry_destroy(lanlayout->entry);
  return ;
}

static gboolean wifi_setting_layout_load_entry_passwd(xmlNode *a_node ,Layout *layout)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;

  xmlNode *cur_node = NULL;
  Entry_Wifi *entry_wifi ;
  int i = 0;
  if(!a_node)
    return FALSE;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next)
  {
    if (cur_node->type == XML_ELEMENT_NODE)
    {
      if(strcmp(cur_node->name,"entry")==0)
      {
        entry_wifi = wifi_create_entry_passwd_from_xml(cur_node , layout);
        if(entry_wifi)
        {
          wifilayout->entry_count++ ;
          if(wifilayout->entrys_passwd)
          {
            Entry_Wifi *pentry ;
            pentry = wifilayout->entrys_passwd->prv ;
            pentry->next = entry_wifi ;
            entry_wifi->prv = pentry ;

            wifilayout->entrys_passwd->prv = entry_wifi ;
            entry_wifi->next = wifilayout->entrys_passwd ;
          }
          else
          {
            wifilayout->entrys_passwd = entry_wifi ;
            entry_wifi->next = entry_wifi ;
            entry_wifi->prv = entry_wifi ;
          }
        }
      }
    }
    //i++;
  }

  return TRUE ;
}

static gboolean wifi_setting_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  xmlNode *cur_node = NULL;
  xmlNode *bg_node = NULL;
  xmlNode *icon_node = NULL;
  xmlNode *entry_net_node = NULL;
  xmlNode *entry_passwd_node = NULL ;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) 
  {
    if (cur_node->type == XML_ELEMENT_NODE)
    {
      printf("node type: Element, name: %s\n", cur_node->name);
      if(strcmp(cur_node->name,"bg")==0)
        bg_node=cur_node;
      else if(strcmp(cur_node->name,"icons")==0)
        icon_node=cur_node;
      else if(strcmp(cur_node->name , "entrys") == 0)
      		entry_net_node = cur_node ;
      else if(strcmp(cur_node->name , "entrys_passwd") == 0)
      		entry_passwd_node = cur_node ;
    }
  }
  if(!bg_node || !icon_node || !entry_net_node )
    return FALSE;

  wifi_setting_layout_load_bg(bg_node,layout);
  wifi_setting_layout_load_icons(icon_node->children,layout);
  wifi_setting_layout_load_tree(layout);
  wifi_setting_layout_load_entry_net_address(entry_net_node->children ,layout);
  wifi_setting_layout_load_entry_passwd(entry_passwd_node->children ,layout);
  hide_wifi_entry(layout);

  if(wifilayout->micon_count>=3)
  {
    return TRUE;
  }

  return FALSE;
}


gboolean wifi_setting_layout_load_config(Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  gboolean ret=FALSE;
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;
  WifiLayout *wifilayout = layout->private ;
  char conf[256];
  sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
  printf("layout->name=%s ,%s\n" ,layout->name , conf);

  doc = xmlReadFile(conf, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr,"error: could not parse file %s\n", conf);
    return ret;
  }
  root_element = xmlDocGetRootElement(doc);

  if(root_element)
  {
    xmlNode *cur_node = NULL;
    for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
      if (cur_node->type == XML_ELEMENT_NODE) {
        ret=TRUE;
        break;
      }
    }
    if(ret)
      ret=wifi_setting_layout_travel_xml(cur_node->children,layout);
  }

  xmlFreeDoc(doc);
  xmlCleanupParser();
  return ret;

}

void hide_wifi_setting_layout_icons(Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  Icon_Wifi* icon_wifi=wifilayout->icons;
  int i  ;

  for (i = 0 ; i < wifilayout->micon_count ; i++)
 	{
    g_object_set(wifilayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    g_object_set(wifilayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);		
  }
}

void hide_wifi_setting_passwd_layout(Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  Icon_Wifi* icon_wifi=wifilayout->icons;

  hide_wifi_setting_layout_icons(layout);
  hide_wifi_entry(layout);
  g_object_set(wifilayout->bg_wifi->pic1 ,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
}

void wifi_setting_layout_reset_icons(Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  Icon_Wifi* icon_wifi=wifilayout->icons;
  int i  ;

  for (i = 0 ; i < wifilayout->micon_count ; i++)
  {
    g_object_set(wifilayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    g_object_set(wifilayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    goo_canvas_item_translate(wifilayout->icons[i].icon,
        wifilayout->icons[i].x,wifilayout->icons[i].y);    
    goo_canvas_item_translate(wifilayout->icons[i].icon_a,
        wifilayout->icons[i].x,wifilayout->icons[i].y);    

    wifilayout->icons[i].x=0;
    wifilayout->icons[i].y=0;
  }
  g_object_set(wifilayout->icons[0].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_set(wifilayout->icons[0].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
}


void show_wifi_net_address(Layout *layout)
{
	printf("~~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	wifilayout->wifistas = WIFI_PASSWD ;

  get_interface(layout);
  printf("wifi_interface = %p\n" ,wifilayout->wifi->my_wifi->wifi_interface[0]);
	read_interface_info(wifilayout->wifi->my_wifi->wifi_interface[0]);	

	show_wifi_entry(layout);
	wifi_setting_layout_reset_icons(layout);
	g_object_set(wifilayout->bg_wifi->pic1,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
}

void show_wifi_list(Layout *layout)
{
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	
	wifi_is_show = WIFI_LAYOUT_UNSHOW ;
	gtk_widget_show(wifilayout->list);
	g_object_set(wifilayout->bg_wifi->pic2,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

}

static gboolean wifi_setting_layout_init(Layout *layout,gpointer data)
{
  WifiLayout *wifilayout = (WifiLayout *)g_malloc0(sizeof (WifiLayout)+1);
  layout->private = (void *)wifilayout ;
  layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

  if(!wifi_setting_layout_load_config(layout))
  {
    fprintf(stderr,"%s:load config err\n",__FUNCTION__);
    //return FALSE;//exit(-1);
  }
  show_wifi_list(layout);
  //show_wifi_net_address(layout);


  return TRUE;
}

static gboolean wifi_setting_layout_clear(Layout *layout,gpointer data)
{
  printf("call %s\n",__FUNCTION__);

  WifiLayout *wifilayout = layout->private ;

  g_object_unref(wifilayout->bg_wifi);	
  g_object_unref(wifilayout->bg_wifi->bg);	
  g_object_unref(wifilayout->bg_wifi->pic1);
  g_object_unref(wifilayout->bg_wifi->pic2);
  g_object_unref(wifilayout->bg_wifi->pixbuf1);
  g_object_unref(wifilayout->bg_wifi->pixbuf2);	

  g_free(wifilayout);
}

void hide_list(Layout *layout)
{
	WifiLayout *wifilayout = layout->private ;
	wifi_is_show = WIFI_LAYOUT_SHOW ;
	gtk_widget_hide(wifilayout->list);
	g_object_set(wifilayout->bg_wifi->pic2,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
}

static void wifi_change_icon(Layout *layout, int icon_id)
{
  WifiLayout *wifilayout = layout->private ;
  int i = 0;
  wifilayout->focus_id = icon_id ;
  printf("micon_count = %d ,icon_id=%d\n" ,wifilayout->micon_count ,icon_id);
  for (i  ; i < wifilayout->micon_count ; i++)
  {
    if(i == icon_id)
    {
      printf("i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(wifilayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      g_object_set(wifilayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      printf("name = %s\n" ,wifilayout->icons[i].name);
    }
    else if(i != icon_id)
    {
      printf("!~i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(wifilayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      g_object_set(wifilayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      printf("name = %s\n" ,wifilayout->icons[i].name);
    }
  }
}

static void wifi_setting_layout_move_icon(Layout *layout,gboolean is_up)
{
	WifiLayout *wifilayout = layout->private ;
	SMS *sms = layout->sms ;
	
  if(is_up)
  {
    wifi_change_icon(layout ,abs((wifiset_focus_id)++)%(wifilayout->micon_count));
    sms->busy = FALSE ;
  }
  else
  {
    wifi_change_icon(layout ,abs((wifiset_focus_id)--)%(wifilayout->micon_count));
    sms->busy = FALSE ;
  }
}

gboolean wifi_dhcp_connect(Layout *layout)
{
	WifiLayout *wifilayout = layout->private ;

					wifi_connect(NULL, layout);
        printf("dialog response dhcp\n");
        wifilayout->wifi->my_wifi->wifi_interface[0]->isstatic = FALSE;
        wifilayout->wifi->my_wifi->wifi_interface[0]->isdhcp = TRUE;
        wirte_interface_info(wifilayout->wifi->my_wifi->wifi_interface[0]);
				//get the passwd and check
        connet(NULL, layout);
}

gboolean wifi_staticip_connect(Layout *layout)
{
	printf("~~~%s\n" ,__FUNCTION__);
	WifiLayout *wifilayout = layout->private ;
	wifi_connect(NULL, layout);
	read_interface_info(wifilayout->wifi->my_wifi->wifi_interface[0]);		//put it in create entry to set the text
	wifi_set_staticip(layout);
	connet(NULL, layout);
}

static gboolean wifi_setting_layout_key_press(Layout * layout,GdkEventKey *event)
{
	printf("~~~%s\n" ,__FUNCTION__);
  WifiLayout *wifilayout = layout->private ;
  Icon_Wifi *icon_wifi;
  SMS *sms = layout->sms ;
  int i;

  if(event->keyval == GDK_Escape)
  {
  	if(wifilayout->wifistas == WIFI_PASSWD)
  	{
			hide_wifi_setting_passwd_layout(layout);
			show_wifi_list(layout);
			wifilayout->wifistas = WIFI_LIST ;
  	}
  	else 
  	{
  		hide_list(layout);
    	sms_change_win_to(layout->sms , LAYOUT_WIN_SETTING);
  	}
  }
  else if(event->keyval == GDK_Up && 
  	wifilayout->wifistas == WIFI_PASSWD && 
  	wifilayout->wifi_selete ==WIFI_ICON_SELETE)
 	{
			printf("up&&\n");
			wifi_setting_layout_move_icon(layout ,TRUE);
  }
  else if(event->keyval == GDK_Down && 
  	wifilayout->wifistas == WIFI_PASSWD && 
  	wifilayout->wifi_selete ==WIFI_ICON_SELETE)
 	{
			printf("down&&&\n");
			wifi_setting_layout_move_icon(layout ,FALSE);
  }
  else if(event->keyval == GDK_Right && 
  	wifilayout->wifistas == WIFI_PASSWD)
  {
    wifilayout->wifi_selete = WIFI_WIDGET_SELETE ;
    printf("selete_stat = %d\n" ,wifilayout->wifi_selete);
  }
  else if(event->keyval == GDK_Left && 
  	wifilayout->wifistas == WIFI_PASSWD)
  {
    wifilayout->wifi_selete = WIFI_ICON_SELETE ;
    printf("selete_stat = %d\n" ,wifilayout->wifi_selete);
  }
  else if(event->keyval == GDK_Return && 
  	wifilayout->wifistas == WIFI_PASSWD && 
  	wifilayout->wifi_selete ==WIFI_ICON_SELETE)
 	{
			printf("cur_icon_id = %d\n" ,wifilayout->focus_id);
			if(wifilayout->focus_id == 1)
			{
					printf("DHCP8888888\n");
					wifilayout->wifi_stats = WIFI_DHCP ;
					wifi_dhcp_connect(layout);
			}
    //STATIC select
    else if (wifilayout->focus_id == 0)
   	{
					printf("STATIC******~~~~\n");
					wifilayout->wifi_stats = WIFI_STATIC ;
					wifi_staticip_connect(layout);
    }
  }
  
  printf("~~~~~~focus_id = %d\n" ,wifilayout->focus_id);
  if((wifilayout->focus_id == 1 || wifilayout->focus_id == 2)
      && wifilayout->wifi_selete ==WIFI_ICON_SELETE 
      && (event->keyval == GDK_Up || event->keyval == GDK_Down ))

  {
    Entry_Wifi *entry_net = wifilayout->entrys_net;
    printf("DHCP\n");
    for(i = 0 ; i < wifilayout->entry_count ; i++)
    {
      printf("entry name = %s \n" ,entry_net->name);
      gtk_widget_set_sensitive(entry_net->entry_wifi ,FALSE);
      entry_net = entry_net->next ;
    }
  }
  if(wifilayout->focus_id == 0 
      && wifilayout->wifi_selete == WIFI_ICON_SELETE 
      && (event->keyval == GDK_Up || event->keyval == GDK_Down))

  {
    Entry_Wifi *entry_net = wifilayout->entrys_net;
    printf("STATIC\n");
    for(i = 0 ; i < wifilayout->entry_count ; i++)
    {
      printf("entry name = %s \n" ,entry_net->name);
      gtk_widget_set_sensitive(entry_net->entry_wifi ,TRUE);
      entry_net = entry_net->next ;
    }
  }
  return TRUE ;
}


Layout *wifi_setting_layout_new(GooCanvasItem * parent,gpointer sms)
{
  Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN,LAYOUT_WIN_SETTING,
      WIFI_SETTING_LAYOUT_NAME,sms);

  layout_set_func(layout,"init",wifi_setting_layout_init);
  layout_set_func(layout,"clear",wifi_setting_layout_clear);
  layout_set_func(layout,"key-press",wifi_setting_layout_key_press);
  return layout;
}

